import { GoodsItem } from '../model/GoodsItem'

export class GoodsModel {
  // 商品数据
  goods: GoodsItem[] = [
    new GoodsItem('羊肉串', 10),
    new GoodsItem('猪肉串', 5),
    new GoodsItem('鸡翅', 15),
  ]

  /**
   * 全选
   */
  isAllSelect: boolean = false

  total: number = 0


  allSelectChange() {
    this.isAllSelect = !this.isAllSelect
    this.goods.forEach(item => {
      item.select = this.isAllSelect
    })
    this.sum()
  }

  singleSelect() {
    this.isAllSelect = this.goods.every(item => item.select)
    this.sum()
  }

  sum () {
    this.total = this.goods.filter(item => item.select).reduce((preItem, item) => preItem += item.price * item.num , 0)
  }
}